﻿using System;
using System.IO;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.IO.Packaging;
using DocumentFormat.OpenXml.Packaging;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            string template = "Finding.docx";
            string finding = "finding.xml";
     

            if (args.Length == 0)
            {
               Console.WriteLine("using defaults..."); // Check for null array
            }
            else if (args.Length == 2)
            {
               Console.WriteLine("parsing args");
               template = args[0];
               finding = args[1];
            }
            else
            {
                Console.WriteLine("Usage: wordgen.exe [template.docx] [finding.xml]");
                Environment.Exit(1);
            }


            string xmlDocRelType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml";
            string officeDocRelType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";

            string docPath = template;
            string xmlDoc = "<blah/>";

            try
            {
                xmlDoc = new StreamReader(finding).ReadToEnd();
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("oops, couldn't find: " + finding + e.ToString());
                Environment.Exit(2);
            }

            PackagePart xmlPart = null;
            PackagePart documentPart = null;

            Uri documentUri = null;
            Uri xmlUri = null;

            //open the package
            try
            {
                using (Package officePackage = Package.Open(docPath, FileMode.Open, FileAccess.ReadWrite))
                {
                    //find the document part
                    foreach (PackageRelationship relationship in officePackage.GetRelationshipsByType(officeDocRelType))
                    {
                        //only one document part
                        documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                        documentPart = officePackage.GetPart(documentUri);
                        break;
                    }

                    //XmlDocument partDoc;
                    //find the custom XML Uri
                    foreach (PackageRelationship relationship in documentPart.GetRelationshipsByType(xmlDocRelType))
                    {
                        //TODO: add namespace check as will have more than one
                        //custom XML in solution
                        xmlUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                        break;
                    }

                    //get the customXML part
                    xmlPart = officePackage.GetPart(xmlUri);

                    //modify the contents of the part
                    Stream outputStream = xmlPart.GetStream(FileMode.Create, FileAccess.ReadWrite);
                    StreamWriter ts = new StreamWriter(outputStream);
                    ts.Write(xmlDoc);
                    ts.Flush();
                    ts.Close();

                    //close our package up
                    officePackage.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error :/ " + e.ToString());
            }

        }
    }
}
